//index.js
//获取应用实例
var app = getApp();
var ImageURL = app.globalData1.ImageURL;
var hostURL = app.globalData1.hostURL;

Page({
  data: {
     postData:{
        userInfo: [],
        goodsList: [], 
     },
    userInfo:[],
    goodsList: [], 
    isNeedLogistics:0, // 是否需要物流信息
    goodsJsonStr:"",
    orderType:"", //订单类型，购物车下单或立即支付下单，默认是购物车，
    beizhu :'',
    userId:'',
    shopList:[],
    ImageURL: app.globalData1.ImageURL
  },
  onShow : function () {
    var that = this;
   //  var shopList = [];
    //立即购买下单
    if ("buyNow"==that.data.orderType){
      var buyNowInfoMem = wx.getStorageSync('buyNowInfo');
      that.data.kjId = buyNowInfoMem.kjId;
   
      if (buyNowInfoMem && buyNowInfoMem.shopList) {
        that.data.shopList = buyNowInfoMem.shopList
      }
    }else{
      //购物车下单
     
    }
    that.setData({
      goodsList: that.data.shopList,
    });
    console.log("购物车下单： ")
    console.log(that.data.shopList)
  },
  onLoad: function (e) {
    var userInfo = wx.getStorageSync('userInfo');//wx.getStorageSync(KEY),从本地缓存中同步获取指定 key 对应的内容
     //var token = wx.getStorageSync('token');
     var that = this;
     that.setData({
        userInfo: userInfo,
     });
     console.log('提交订单')
     console.log(userInfo)

     var cartList = wx.getStorageSync('cartList');
      console.log(cartList)
   
    //var that = this;
    //显示收货地址标识
    that.setData({
       shopList: cartList,
      isNeedLogistics: 1,
      orderType: e.orderType
    });
  },

  inputbeizhu: function (e) {
     this.setData({
        beizhu: e.detail.value,
     })
     var beizhu = []
     beizhu = this.data.beizhu
        this.setData({
           beizhu: beizhu
        })
        console.log(beizhu)
  },

  createOrder:function (e) {
    //wx.showLoading();
   var that = this;
    var userInfo = wx.getStorageSync('userInfo');//wx.getStorageSync(KEY),从本地缓存中同步获取指定 key 对应的内容
    var token = wx.getStorageSync('token');
    that.setData({
       userInfo: userInfo,
    });
    console.log(userInfo)
    var beizhu = that.data.beizhu
    var userInfo = that.data.userInfo
    var realname=userInfo.realname
    var phone = userInfo.phone
    var userId = userInfo.id

    that.setData({
       userId: userId
    })
    var postData = {
       realname: that.data.userInfo.realname,
       phone: that.data.userInfo.phone,
       userId: that.data.userInfo.id,
       goodsList:that.data.goodsList,
       beizhu: that.data.beizhu
    };

    console.log('总数据')
    console.log(postData)
 

   //  var goodsList = that.data.goodsList
   //  console.log("ndjgsab")
   //  console.log(goodsList)
    wx.request({
       url: hostURL + 'sharedplatform-web/orderMaster/create.action',
      method:'get',
      header: {
         'Accept': 'application/json'
      },
      data: {
         orderList: postData

      }, // 设置请求的 参数
      success: (res) =>{
        wx.hideLoading();
        if(res.data.errno==1)
        {
           wx.showModal({
            title: '下单失败',      
          })
          return;
        }
        else if (res.data.errno == 0){

            // var orderId=res.data.orderId
            // wx.getStorage({
            //    key: 'userInfo',
            //    success: function (res) {
            //       console.log(res.data)
            //       that.setData({
            //          userId:res.data.id
            //       })
            //    }
            // })
            
           wx.redirectTo({ 
              url: "/pages/order-list/index?userId=" + that.data.userId

           });
        }
        
        }

    }) 
  },
})
